using System;

namespace ntools.NetduinoGo.PlusModule.NetduinoGo.Framework
{
    public class NetworkStream : System.IO.Stream
    {
        private byte socket;
        private byte instance;

        public NetworkStream(Socket socket)
        {
            this.socket = socket.Instance;
            this.instance = Plus.Link.Call(Link.INSTANCE_MAIN, Link.COMMAND_MAIN_CREATENETWORKSTREAM, new byte[] { this.socket })[0];
        }

        public override bool CanRead
        {
            get
            {
                return 1 == Plus.Link.Call(Link.INSTANCE_NETWORKSTREAM, Link.COMMAND_NETWORKSTREAM_CANREAD, new byte[] { this.instance })[0];
            }
        }

        public override int ReadByte()
        {
            return (int)Plus.Link.Call(Link.INSTANCE_NETWORKSTREAM, Link.COMMAND_NETWORKSTREAM_READBYTE, new byte[] { this.instance })[0];
        }

        public override int Read(byte[] buffer, int index, int length)
        {
            byte[] read = Plus.Link.Call(Link.INSTANCE_NETWORKSTREAM, Link.COMMAND_NETWORKSTREAM_READ, new byte[] { this.instance, (byte)length });

            Array.Copy(read, 0, buffer, index, read.Length);

            return read.Length;
        }

        public new void Dispose()
        {
            Plus.Link.Call(Link.INSTANCE_NETWORKSTREAM, Link.COMMAND_NETWORKSTREAM_DISPOSE, new byte[] { this.instance });
            base.Dispose();
        }

        public override bool CanSeek
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanWrite
        {
            get { throw new NotImplementedException(); }
        }

        public override void Flush()
        {
            throw new NotImplementedException();
        }

        public override long Length
        {
            get { throw new NotImplementedException(); }
        }

        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override long Seek(long offset, System.IO.SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
    }
}
